package com.ruoyi.project.wx.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.wx.domain.CmsLinkDO;
import com.ruoyi.project.wx.service.CmsLinkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 连接地址Controller
 *
 * @author ruoyi
 * @date 2021-04-15
 */
@RestController
@RequestMapping("/wx/wxCmsLink")
public class CmsLinkController extends BaseController
{
	@Autowired
	private CmsLinkService cmsLinkService;

	/**
	 * 查询连接地址列表
	 */
	@ResponseBody
	@PreAuthorize("@ss.hasPermi('wx:wxCmsLink:list')")
	@GetMapping("/list")
	public TableDataInfo list(CmsLinkDO cmsLink)
	{
		startPage();
		Map<String, Object> stringObjectMap = BeanUtil.beanToMap(cmsLink);
		stringObjectMap.put("delFlag","0");
		stringObjectMap.put("sort","ca_at");
		stringObjectMap.put("order","desc");
		if(cmsLink.getStatus()!=null){
			stringObjectMap.put("status",cmsLink.getStatus()+"");
		}
		List<CmsLinkDO> list = cmsLinkService.list(stringObjectMap);
		return getDataTable(list);
	}

	/**
	 * 导出连接地址列表
	 */
	@PreAuthorize("@ss.hasPermi('wx:wxCmsLink:export')")
	@Log(title = "连接地址", businessType = BusinessType.EXPORT)
	@GetMapping("/export")
	public AjaxResult export(CmsLinkDO cmsLink)
	{
		Map<String, Object> stringObjectMap = BeanUtil.beanToMap(cmsLink);
		List<CmsLinkDO> list = cmsLinkService.list(stringObjectMap);
		ExcelUtil<CmsLinkDO> util = new ExcelUtil<CmsLinkDO>(CmsLinkDO.class);
		return util.exportExcel(list, "wxCmsLink");
	}

	/**
	 * 获取连接地址详细信息
	 */
	@PreAuthorize("@ss.hasPermi('wx:wxCmsLink:query')")
	@GetMapping(value = "/{id}")
	public AjaxResult getInfo(@PathVariable("id") String id)
	{
		return AjaxResult.success(cmsLinkService.get(id));
	}

	/**
	 * 新增连接地址
	 */
	@PreAuthorize("@ss.hasPermi('wx:wxCmsLink:add')")
	@Log(title = "连接地址", businessType = BusinessType.INSERT)
	@PostMapping
	public AjaxResult add(@RequestBody CmsLinkDO cmsLink)
	{
		cmsLink.setId(IdUtil.simpleUUID());
		cmsLink.setDelFlag(0);
		cmsLink.setCaAt(new Date());
		return toAjax(cmsLinkService.save(cmsLink));
	}

	/**
	 * 修改连接地址
	 */
	@PreAuthorize("@ss.hasPermi('wx:wxCmsLink:edit')")
	@Log(title = "连接地址", businessType = BusinessType.UPDATE)
	@PutMapping
	public AjaxResult edit(@RequestBody CmsLinkDO cmsLink)
	{
		cmsLink.setUpAt(new Date());
		return toAjax(cmsLinkService.update(cmsLink));
	}

	/**
	 * 删除连接地址
	 */
	@PreAuthorize("@ss.hasPermi('wx:wxCmsLink:remove')")
	@Log(title = "连接地址", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
	public AjaxResult remove(@PathVariable String[] ids)
	{
		if(ids.length>0){
			for (String string : ids) {
				CmsLinkDO cmsLink = cmsLinkService.get(string);
				cmsLink.setDelFlag(1);
				cmsLink.setUpAt(new Date());
				cmsLinkService.update(cmsLink);
			}
		}
		return toAjax(ids.length);
		//return toAjax(cmsLinkService.deleteCmsLinkByIds(ids));
	}
}
